package carassius.GUI;

import carassius.DAL.ActionRow;
import carassius.DAL.AquariumRow;
import carassius.DAL.Database;
import carassius.DAL.WaterMeasurementRow;
import carassius.DAL.WaterRefreshmentRow;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.GregorianCalendar;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

/**
 *
 * @author siebz0r
 */
public class WaterActionList
		extends ListWithMenu
{
	private Frame parentFrame;
	private AquariumRow selectedAquarium;
	private GregorianCalendar selectedDate;

	public WaterActionList()
	{
		super(new JPopupMenu());

		ResourceBundle resourceBundle = ResourceBundle.getBundle("carassius.GUI.resources.ListWithMenu");

		JMenu addMenuItem = new JMenu();
		addMenuItem.setText(resourceBundle.getString("Add"));
		addMenuItem.setFont(addMenuItem.getFont().deriveFont(Font.PLAIN));
		POPUP_MENU.add(addMenuItem);

		ResourceBundle objectsResourceBundle = ResourceBundle.getBundle("carassius.resources.ObjectNames");

		JMenuItem addWaterMeasurementMenuItem = new JMenuItem();
		addWaterMeasurementMenuItem.setAction(new addWaterMeasurementAction());
		addWaterMeasurementMenuItem.setText(objectsResourceBundle.getString("WaterMeasurement"));
		addWaterMeasurementMenuItem.setFont(addWaterMeasurementMenuItem.getFont().deriveFont(Font.PLAIN));
		addMenuItem.add(addWaterMeasurementMenuItem);

		JMenuItem addWaterRefreshmentMenuItem = new JMenuItem();
		addWaterRefreshmentMenuItem.setAction(new addWaterRefreshmentAction());
		addWaterRefreshmentMenuItem.setText(objectsResourceBundle.getString("WaterRefreshment"));
		addWaterRefreshmentMenuItem.setFont(addWaterRefreshmentMenuItem.getFont().deriveFont(Font.PLAIN));
		addMenuItem.add(addWaterRefreshmentMenuItem);

		JMenuItem removeMenuItem = new JMenuItem();
		removeMenuItem.setAction(new removeAction());
		removeMenuItem.setText(resourceBundle.getString("Remove"));
		removeMenuItem.setFont(removeMenuItem.getFont().deriveFont(Font.PLAIN));
		POPUP_MENU.add(removeMenuItem);

		JMenuItem editMenuItem = new JMenuItem();
		editMenuItem.setAction(new editAction());
		editMenuItem.setText(resourceBundle.getString("Edit"));
		editMenuItem.setFont(editMenuItem.getFont().deriveFont(Font.PLAIN));
		POPUP_MENU.add(editMenuItem);
	}

	private Frame getParentFrame()
	{
		if (parentFrame == null)
		{
			Container parent = getParent();
			while (parent != null)
			{
				if (parent instanceof Frame)
				{
					break;
				}
				parent = parent.getParent();
			}
			if (parent instanceof Frame)
			{
				parentFrame = (Frame) parent;
			}
		}
		return parentFrame;
	}

	public void setSelectedAquarium(AquariumRow row)
	{
		selectedAquarium = row;
	}

	public void setSelectedDate(GregorianCalendar date)
	{
		selectedDate = date;
	}

	private void addWaterMeasurement()
	{
		if (selectedAquarium != null && selectedDate != null)
		{
			java.sql.Date date = new java.sql.Date(selectedDate.getTimeInMillis());

			WaterActionForm form = new WaterActionForm(getParentFrame(), (WaterMeasurementRow) null, selectedAquarium, date);
			form.setVisible(true);
		}
	}

	private void addWaterRefreshment()
	{
		if (selectedAquarium != null && selectedDate != null)
		{
			java.sql.Date date = new java.sql.Date(selectedDate.getTimeInMillis());

			WaterActionForm form = new WaterActionForm(getParentFrame(), (WaterRefreshmentRow) null, selectedAquarium, date);
			form.setVisible(true);
		}
	}

	@Override
	void add()
	{
	}

	@Override
	void remove()
	{
		if (getSelectedValue() instanceof ActionRow)
		{
			if (JOptionPane.showConfirmDialog(this,
					ResourceBundle.getBundle("carassius.resources.Messages").getString("DeleteActionQuestion"),
					ResourceBundle.getBundle("carassius.resources.Messages").getString("DeleteActionQuestionTitle"),
					JOptionPane.YES_NO_OPTION,
					JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION)
			{
				try
				{
					if (getSelectedValues().length == 1)
					{
						if (getSelectedValue() instanceof WaterMeasurementRow)
						{
							Database.getWaterMeasurementTable().delete((ActionRow) getSelectedValue());
						}
						else if (getSelectedValue() instanceof WaterRefreshmentRow)
						{
							Database.getWaterRefreshmentTable().delete((WaterRefreshmentRow) getSelectedValue());
						}
					}
					else
					{
						for (Object o : getSelectedValues())
						{
							if (o instanceof WaterMeasurementRow)
							{
								Database.getWaterMeasurementTable().delete((WaterMeasurementRow) getSelectedValue());
							}
							else if (o instanceof WaterRefreshmentRow)
							{
								Database.getWaterRefreshmentTable().delete((WaterRefreshmentRow) getSelectedValue());
							}
						}
					}
				}
				catch (SQLException ex)
				{
					JOptionPane.showMessageDialog(getParentFrame(),
							ResourceBundle.getBundle("carassius.resources.Messages").getString("DeleteActionError"),
							ResourceBundle.getBundle("carassius.resources.Messages").getString("DeleteActionErrorTitle"),
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}

	@Override
	void edit()
	{
		if (getSelectedValues().length == 1)
		{
			if (getSelectedValue() instanceof WaterRefreshmentRow)
			{
				WaterActionForm form = new WaterActionForm(getParentFrame(), (WaterRefreshmentRow) getSelectedValue());
				form.setVisible(true);
			}
			else if (getSelectedValue() instanceof WaterMeasurementRow)
			{
				WaterActionForm form = new WaterActionForm(getParentFrame(), (WaterMeasurementRow) getSelectedValue());
				form.setVisible(true);
			}
		}
	}

	private class addWaterMeasurementAction
			extends AbstractAction
			implements Action
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			addWaterMeasurement();
		}
	}

	private class addWaterRefreshmentAction
			extends AbstractAction
			implements Action
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			addWaterRefreshment();
		}
	}
}
